package com.itheima.web.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.domain.db.Admin;
import com.itheima.web.manager.AdminManeger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
public class AdminController {
    @Autowired
    private AdminManeger adminManeger;
    //用户登录验证码图片  void是因为 我们要返回一个图片数据
    @GetMapping("/system/users/verification")
    public void verification(String uuid, HttpServletResponse response) throws IOException {
        LineCaptcha lineCaptcha=adminManeger.verification(uuid);
        lineCaptcha.write(response.getOutputStream());
    }
    //登录校验
    @PostMapping("/system/users/login")
    public ResponseEntity login(@RequestBody Map<String ,String> map) throws IOException {
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        return adminManeger.login(username,password,verificationCode,uuid);
    }

    //前端携带token 获取用户基本信息
    @PostMapping("/system/users/profile")
    public ResponseEntity profile(@RequestHeader("Authorization")String token){
        Admin admin = adminManeger.profile(token);
        return ResponseEntity.ok(admin);

    }
    //登出
    @PostMapping("/system/users/logout")
    public ResponseEntity logout(@RequestHeader("Authorization")String token){
        return adminManeger.logout(token);
    }

}
